/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 
package buco.core.api;

import java.util.*;
import java.util.Map.Entry;

public class BundleWire implements MetadataVisitable {
	
	private Bundle from; 
	private Bundle to; 
	private boolean isWireActive;
	
	private HashMap<Requirement, ArrayList<Capability>> mapping;
	
	public BundleWire(Bundle from, Bundle to) {
		this.mapping = new HashMap<Requirement, ArrayList<Capability>>(); 
		this.from = from; 
		this.to = to; 				
	}

	public boolean isWireActive() {
		return isWireActive;
	}

	public void setIsActive(boolean isActive) {
		this.isWireActive = isActive;
	}
	
	public void addMatch(Requirement requirement, Capability capability) {
		ArrayList<Capability> list = null; 
		if(!this.mapping.containsKey(requirement)) {
			list = new ArrayList<Capability>();
			mapping.put(requirement, list);
		}
		else {
			list = mapping.get(requirement); 
		}			
		list.add(capability);  
	}

	public Bundle getFrom() {
		return from;
	}

	public Bundle getTo() {
		return to;
	}
	
	public List<Requirement> getRequirements() {
		ArrayList<Requirement> list = new ArrayList<Requirement>(); 
		for(Requirement req : this.mapping.keySet()) {
			list.add(req); 
		}
		return list; 			
	}
	
	public List<Capability> getCapabilities() {
		ArrayList<Capability> list = new ArrayList<Capability>(); 
		for(Entry<Requirement, ArrayList<Capability>> entry : this.mapping.entrySet()) { 
			list.addAll(entry.getValue()); 
		}
		return list;
	}
	
	@Override
	public void accept(MetadataVisitor visitor) { 
		List<Requirement> reqs = getRequirements(); 
		List<Capability> caps = getCapabilities(); 
		
		for(Requirement req : reqs) { 
			req.accept(visitor); 
		}
		
		for(Capability cap : caps) { 
			cap.accept(visitor); 
		}
	}

}
